
function add_cart(commodity_code,qty,commodity_name,price,cart){
	try {
		$.ajax ({
			url: "/zswebshop/index.php/cart/add_cart_aj/",
			type: "POST",
			dataType: "json",
			data: {
				'commodity_code' : commodity_code,
			  	'qty': qty,
			  	'commodity_name': commodity_name,
			  	'price': price
			},
			success: function(data, textStatus){
				if(data.result== 0){
					if(cart=='true'){
						location.href = "/zswebshop/index.php/cart/";
					}else{
						alert(commodity_name+"がカートに入りました。");	
					}
					
				}else{
					alert("failure!");
				}
			},
			error: function(xhr, textStatus, errorThrown){
			      // エラー処理
				alert(textStatus);
		    }

		});
	} catch(err) {
		alert(err);
	}
}
function add_cart_test(commodity_code,qty,commodity_name,price){
	var redirect = '/zswebshop/index.php/cart/add_cart/';
	postAndRedirect(redirect, {
		'commodity_code': commodity_code, 
		'qty': qty,
		'commodity_name': commodity_name,
		'price': price
		}
	);
}


function postAndRedirect(url, postData)
{
    var postFormStr = "<form method='POST' action='" + url + "'>\n";

    for (var key in postData)
    {
        if (postData.hasOwnProperty(key))
        {
            postFormStr += "<input type='hidden' name='" + key + "' value='" + postData[key] + "'></input>";
        }
    }

    postFormStr += "</form>";

    var formElement = $(postFormStr);

    $('body').append(formElement);
    $(formElement).submit();
}

